*******************************************************************************
* > Loop over subgroups
*******************************************************************************

foreach subgroup in All Democrat Republican {

*******************************************************************************
* >> Loop over datasets
*******************************************************************************

foreach dataset in gallup gss {

*******************************************************************************
* Load data
*******************************************************************************

use dataset_`dataset', clear
	
*******************************************************************************
* Keep the subgroup
*******************************************************************************

keep if subgroup == "`subgroup'"

*******************************************************************************
* Label lines if it's the first plot
*******************************************************************************

local mylabel "Police"
if "`dataset'" == "gss" local mylabel "Law Enforcement"

local legend1 legend(off)
if "`subgroup'" == "All" {
	local legend1 legend(order(1 ///
		"{fontface monospace:{bf:`mylabel'}}" ///
		2 "{fontface monospace:Control Series}") ///
		symysize(0) symxsize(0) ring(0) pos(11) cols(1) ///
		region(lwid(none)))	
}

*******************************************************************************
* Set params based on data set
*******************************************************************************

if "`dataset'" == "gallup" {
	rename conf Y
	local mytitle2 "Confidence (Gallup)"
	local treated_target "police"
}
if "`dataset'" == "gss" {
	rename nat Y
	local mytitle2 "Spending (GSS)"
	local treated_target "crimy"
}

*******************************************************************************
* Make lags
*******************************************************************************

su year
local mymin = r(min)

local lags
levelsof year if year < 2014, local(levels)
foreach i of local levels {
	egen Y`i'm = mean(Y) if year == `i', by(target)
	egen Y`i' = mean(Y`i'm), by(target)
	drop Y`i'm
	local lags Y`i'
}

*******************************************************************************
* Loop over placebo series 
*******************************************************************************

encode target, gen(id)
tsset id year
local i = 1
levelsof target, local(levels)
local nlevels = r(r)
foreach l of local levels {
	
	su id if target == "`l'"
	local treated = r(mean)

*******************************************************************************
* Do the synth
*******************************************************************************

	#delimit;

	synth 
		Y `lags',
			trunit(`treated')
			trperiod(2014)   
			keep(tmp_`i', replace)
			;

	#delimit cr
		
	preserve
		use tmp_`i', clear
		gen target = "`l'"
		save tmp_`i', replace
	restore

	local i = `i' + 1
		
}

*******************************************************************************
* Graph RMSPE (one-sided test)
*******************************************************************************

use tmp_1, clear
erase tmp_1.dta
forvalues i = 2(1)`nlevels' {
	append using tmp_`i'
	erase tmp_`i'.dta
}

gen D = _Y_treat - _Y_syn
sort target _time
encode target, gen(targetid)

gen DD = D
if "`subgroup'" != "Republican" replace DD = 0 if DD > 0 & _time >= 2014
if "`subgroup'" == "Republican" replace DD = 0 if DD < 0 & _time >= 2014
gen pre_mspem = DD^2 if _time < 2014
egen pre_mspe = mean(pre_mspem), by(target)
gen post_mspem = DD^2 if _time >= 2014
egen post_mspe = mean(post_mspem), by(target)

gen ratio_mspe = sqrt(post_mspe) / sqrt(pre_mspe)

collapse ratio, by(target)
sort ratio
gen index = _n

if "`dataset'" == "gallup" {
	replace target = subinstr(target, "_", " ", 1)
	replace target = proper(target)
	replace target = "Supreme Court" if target == "Sup Ct"
	replace target = "TV News" if target == "Tv News"
}

if "`dataset'" == "gss" {
	replace target = "Foreign aid" if target == "aid"
	replace target = "Foreign aid alt" if target == "aidy"
	replace target = "Military" if target == "arms"
	replace target = "Military alt" if target == "armsy"
	replace target = "Cities" if target == "city"
	replace target = "Cities alt" if target == "cityy"
	replace target = "Crime" if target == "crime"
	replace target = "Law enforcement" if target == "crimy"
	replace target = "Drugs" if target == "drug"
	replace target = "Drugs alt" if target == "drugy"
	replace target = "Education" if target == "educ"
	replace target = "Education alt" if target == "educy"
	replace target = "Environment" if target == "envir"
	replace target = "Environment alt" if target == "enviy"
	replace target = "Welfare" if target == "fare"
	replace target = "Welfare alt" if target == "farey"
	replace target = "Health" if target == "heal"
	replace target = "Health alt" if target == "healy"
	replace target = "Transportation" if target == "mass"
	replace target = "Parks and rec" if target == "park"
	replace target = "Blacks" if target == "race"
	replace target = "Blacks alt" if target == "racey"
	replace target = "Roads" if target == "road"
	replace target = "Social Security" if target == "soc"
	replace target = "Space" if target == "spac"
	replace target = "Space alt" if target == "spacy"
}

replace target = target + (15-length(target))*" "
replace target = "{fontface monospace:" + target + "}"

labmask index, values(target)

#delimit;

gr tw
	(sc index ratio if regex(target, "Law enforcement") | 
		regex(target, "Police"), 
		col(black) msym(O))
	(sc index ratio if !regex(target, "Law enforcement") & 
		!regex(target, "Police"), 
		col(black) msym(Oh))
	, 
		name(g`dataset', replace)
		ylab(1(1)`nlevels', angle(horiz) valuelabel labsize(vsmall))
		plotregion(style(none))
		ytitle("")
		xtitle("Postperiod RMSPE / Preperiod RMSPE", size(small))
		legend(off)
		title("`mytitle2'")		
		;
		
#delimit cr

*******************************************************************************
* >> Close dataset loop
*******************************************************************************

}

*******************************************************************************
* Combine plots for each dataset
*******************************************************************************

local mytitle `subgroup'
if "`subgroup'" != "All" local mytitle = "`subgroup's" 

gr combine ggallup ggss, name(G`subgroup', replace) title(`mytitle')

*******************************************************************************
* > Close subgroup loop
*******************************************************************************

}

*******************************************************************************
* Combine plots for each subgroup
*******************************************************************************

gr combine GAll GDemocrat GRepublican, rows(3) ysize(4) xsize(3) ///
	imargin(zero)

*******************************************************************************
* Save
*******************************************************************************

gr export "figure_lags_last_rmspe.pdf", replace

*******************************************************************************
* End
*******************************************************************************
